package com.example.fourtestapp.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.fourtestapp.R;
import com.example.fourtestapp.entity.MilieuInfo;
import com.example.fourtestapp.utils.HttpUtils;
import com.example.fourtestapp.utils.ParamUtlis;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MilieuService extends Service {

    private SharedPreferences sp;
    private MilieuInfo info;
    private NotificationManager manager;

    private boolean quit = false;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        final String json = new ParamUtlis()
                .setParam("UserName", "user1")
                .build();
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        sp = getSharedPreferences("milieuYuZhi", 0);
        new Thread() {
            @Override
            public void run() {
                while (!quit) {
                    try {
                        HttpUtils.Post("/GetAllSense", json, new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                e.printStackTrace();
                            }
                            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                Log.i("aaa", "拿到数据");
                                String data = response.body().string();
                                info = new Gson().fromJson(data, MilieuInfo.class);
                                EventBus.getDefault().post(info);

                                String temp = sp.getString("temp", "");
                                if (info.getTemp() > Integer.parseInt(temp)) {
                                    Notification notification =
                                            new Notification.Builder(getApplicationContext())
                                            .setSmallIcon(R.drawable.ic_launcher_foreground)
                                            .setContentTitle("【温度】告警")
                                            .setContentText("当前值" + info.getTemp() + "/" + temp + "(阈值)")
                                            .setTicker("阈值告警")
                                            .build();
                                    manager.notify(0, notification);
                                }

                                String sd = sp.getString("sd", "");
                                if (info.getHumidity() > Integer.parseInt(sd)) {
                                    Notification notification =
                                            new Notification.Builder(getApplicationContext())
                                            .setSmallIcon(R.drawable.ic_launcher_foreground)
                                            .setContentTitle("【湿度】告警")
                                            .setContentText("当前值" + info.getHumidity() + "/" + sd + "(阈值)")
                                            .setTicker("阈值告警")
                                            .build();
                                    manager.notify(1, notification);
                                }

                                String light = sp.getString("light", "");
                                if (info.getLightIntensity() > Integer.parseInt(light)) {
                                    Notification notification =
                                            new Notification.Builder(getApplicationContext())
                                            .setSmallIcon(R.drawable.ic_launcher_foreground)
                                            .setContentTitle("【光照强度】告警")
                                            .setContentText("当前值" + info.getLightIntensity() +
                                                    "/" + light + "(阈值)")
                                            .setTicker("阈值告警")
                                            .build();
                                    manager.notify(2, notification);
                                }

                                String co2 = sp.getString("co2", "");
                                if (info.getCo2() > Integer.parseInt(co2)) {
                                    Notification notification =
                                            new Notification.Builder(getApplicationContext())
                                            .setSmallIcon(R.drawable.ic_launcher_foreground)
                                            .setContentTitle("【二氧化碳浓度】告警")
                                            .setContentText("当前值" + info.getCo2() + "/" + co2 +
                                                    "(阈值)")
                                            .setTicker("阈值告警")
                                            .build();
                                    manager.notify(3, notification);
                                }

                                String pm = sp.getString("pm", "");
                                if (info.get_$Pm2594() > Integer.parseInt(pm)) {
                                    Notification notification =
                                            new Notification.Builder(getApplicationContext())
                                            .setSmallIcon(R.drawable.ic_launcher_foreground)
                                            .setContentTitle("【PM2.5浓度】告警")
                                            .setContentText("当前值" + info.get_$Pm2594() + "/" + pm + "(阈值)")
                                            .setTicker("阈值告警")
                                            .build();
                                    manager.notify(4, notification);
                                }

                                String road = sp.getString("road", "");
                                if (info.getRoad() > Integer.parseInt(road)) {
                                    Notification notification =
                                            new Notification.Builder(getApplicationContext())
                                            .setSmallIcon(R.drawable.ic_launcher_foreground)
                                            .setContentTitle("【道路状况】告警")
                                            .setContentText("当前值" + info.getRoad() + "/" + road + "(阈值)")
                                            .setTicker("阈值告警")
                                            .build();
                                    manager.notify(5, notification);
                                }
                            }
                        });
                        Thread.sleep(3000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        quit = true;
        super.onDestroy();
    }
}
